﻿/*
* 该文件主要实现了登录注册功能
*/
//完工
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "manager.h"
#include "employee.h"
#include "customer.h"
#define MAX_NAME_LEN 19
#define MAX_PASS_LEN 19
//这里是我的本地文件的路径，注意修改
const char* MANAGER_FILE = "D:\\Hotel\\Try1 - test1 - fix1_new\\manager.txt";
const char* EMPLOYEE_FILE = "D:\\Hotel\\Try1 - test1 - fix1_new\\employee.txt";
const char* CUSTOMER_FILE = "D:\\Hotel\\Try1 - test1 - fix1_new\\customer.txt";

//这是再版，这次，将所有的注册都封装到一个函数中，减少代码冗余
void log_menu();
void register_menu();
void register_fun(int choice);
void login(int choice);

//此函数用于打印菜单
void Menu()
{
	printf("\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||          欢迎来到酒店管理系统             ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||          请选择您的操作：                 ||\n");
	printf("||                                           ||\n");
	printf("||              1. 用户登录                  ||\n");
	printf("||              2. 新用户注册                ||\n");
	printf("||              0. 退出系统                  ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");

	int choice;
	printf("\n>>> 请输入选项 (0-2): ");
	scanf("%d", &choice); 
	while (1) 
	{
		if (choice < 0 || choice > 2) 
		{
			// 清空输入缓冲区防止卡死
			while (getchar() != '\n');
			printf("||		输入无效，请重新输入 (0-2): ");
			continue;
		}
		break;
	}

	switch (choice) 
	{
	case 1:
		log_menu();
		break;
	case 2:
		register_menu();
		break;
	case 0:
		printf("\n||		   感谢使用，再见！	     ||\n");
		printf("===============================================\n\n");
		exit(0);
	}
}




//登录主界面
void log_menu() 
{
	printf("\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||             登录身份选择                  ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||           请选择您的登录身份：            ||\n");
	printf("||                                           ||\n");
	printf("||              1. 管理员                    ||\n");
	printf("||              2. 酒店员工                  ||\n");
	printf("||              3. 顾客                      ||\n");
	printf("||              0. 退回上一级菜单            ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");

	int choice;
	printf("\n>>> 请选择身份/退出页面 (0-3): ");
	scanf("%d", &choice); 
	while (1) 
	{
		switch (choice)
		{
		case 1:
		case 2:
		case 3:
			login(choice); // 调用登录函数
			break;
		case 0:
			Menu(); // 返回主菜单
			break;
		default:
			// 清空输入缓冲区
			while (getchar() != '\n');
			printf("||		输入错误，请输入有效选项 (0-3): ");
			scanf("%d", &choice);
			continue;
		}
	}  
}
//登录功能实现
void login(int choice)
{
	int i = 0;
	char name[20];
	char password[20];
	int found = 0; // 判断是否存在用户或者账号密码错误
	//优化页面显示
	printf("\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||             用户登录                      ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");
	printf(">>> 请输入用户名/昵称:");
	scanf("%19s", name, MAX_NAME_LEN); 
	printf(">>> 请输入密码:");
	for (i = 0; i < MAX_PASS_LEN; i++)
	{
		password[i] = _getch();
		if (password[i] == '\r' || password[i] == '\n')
		{ // 检测回车键
			password[i] = '\0'; // 结束字符串
			break;
		}
		else if (password[i] == '\b')
		{ // 检测退格键
			if (i > 0)
			{
				i--; // 回退一个位置
				printf("\b \b"); // 删除屏幕上的星号
			}
		}
		else
		{
			printf("*"); // 显示星号
		}
	}
	password[i] = '\0'; // 确保字符串以null字符结尾
	printf("\n");
	//scanf("%19s", password, sizeof(password)); // 使用 sizeof 获取数组大小
	FILE* fp = NULL;
	// 从文件中读取用户信息
	switch (choice)
	{
	case 1:
		fp = fopen(MANAGER_FILE, "r");
		break;
	case 2:
		fp = fopen(EMPLOYEE_FILE, "r");
		break;
	case 3:
		fp = fopen(CUSTOMER_FILE, "r");
		break;
	}

	if (fp == NULL) {
		printf("||			对不起，现在无法查询信息		||\n");
		return;
	}

	// 匹配用户输入的账号密码是否和文件中的账号密码一致
	char name1[20];
	char password1[20];
	while (fscanf(fp, "%19s %19s", name1, password1) == 2) {
		// 确保字符串以空字符结尾
		name1[19] = '\0';
		password1[19] = '\0';

		if (strcmp(name, name1) == 0 && strcmp(password, password1) == 0) {
			printf("\n\n||		登录成功!!!		||\n");
			// 登录成功后，跳转到对应菜单
			switch (choice)
			{
			case 1:
				menu_Manager();
				break;
			case 2:
				front_desk_menu();
				break;
			case 3:
				menu_Customer();
				break;
			}
			found = 1;
			break;
		}
	}
	if (found == 0) 
	{
		system("cls");
		printf("\n");
		printf("===============================================\n");
		printf("||         用户名或密码错误，请重新输入      ||\n");
		printf("===============================================\n");
		login(choice);
	}
	fclose(fp); // 关闭文件


}

//注册页面
void register_menu() 
{
	printf("\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||             注册身份选择                  ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||           请选择您的注册身份：            ||\n");
	printf("||                                           ||\n");
	printf("||              1. 管理员                    ||\n");
	printf("||              2. 酒店员工                  ||\n");
	printf("||              3. 顾客                      ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");

	int choice;
	printf("\n>>> 请选择注册类型 (1-3): ");
	while (1) 
	{
		if (scanf("%d", &choice) != 1 || choice < 1 || choice > 3) 
		{
			// 清空输入缓冲区
			while (getchar() != '\n');
			printf("||		输入错误，请输入有效选项 (1-3): ");
			continue;
		}
		break;
	}

	register_fun(choice);  // 假设已实现注册处理函数
}
//注册功能实现
void register_fun(int choice)
{
	U user1;
	int i = 0;
	char confirm_password[20];
	//优化页面显示
	printf("\n");
	printf("===============================================\n");
	printf("||                                           ||\n");
	printf("||             用户注册                      ||\n");
	printf("||                                           ||\n");
	printf("===============================================\n");
	printf(">>> 请输入用户名/昵称:");
	scanf("%19s", user1.name, MAX_NAME_LEN); // 指定缓冲区大小
	printf(">>> 请输入密码:");
	// 使用getch()函数读取密码，不显示实际字符
	for (i = 0; i < MAX_PASS_LEN; i++) 
	{
		user1.password[i] = _getch();
		if (user1.password[i] == '\r' || user1.password[i] == '\n') 
		{ // 检测回车键
			user1.password[i] = '\0'; // 结束字符串
			break;
		}
		else if (user1.password[i] == '\b') 
		{ // 检测退格键
			if (i > 0) 
			{
				i--; // 回退一个位置
				printf("\b \b"); // 删除屏幕上的星号
			}
		}
		else 
		{
			printf("*"); // 显示星号
		}
	}
	user1.password[i] = '\0'; // 确保字符串以null字符结尾

	printf("\n>>> 请再次输入你的密码:");
	for (i = 0; i < MAX_PASS_LEN; i++) {
		confirm_password[i] = _getch();
		if (confirm_password[i] == '\r' || confirm_password[i] == '\n') {
			confirm_password[i] = '\0';
			break;
		}
		else if (confirm_password[i] == '\b') {
			if (i > 0) {
				i--;
				printf("\b \b");
			}
		}
		else {
			printf("*");
		}
	}
	confirm_password[i] = '\0';
	printf("\n");
	if (strcmp(user1.password, confirm_password) == 0)
	{
		//保存用户信息到文件中
		FILE* fp = NULL;
		// 从文件中读取用户信息
		switch (choice)
		{
		case 1:
			fp = fopen(MANAGER_FILE, "a");
			break;
		case 2:
			fp = fopen(EMPLOYEE_FILE, "a");
			break;
		case 3:
			fp = fopen(CUSTOMER_FILE, "a");
			break;
		}

		if (fp == NULL)
		{
			printf("文件打开失败\n");
			exit(0);
		}
		else
		{
			fprintf(fp, "%s %s\n", user1.name, user1.password);
			printf("\n");
			printf("===============================================\n");
			printf("||         注册成功，欢迎使用本系统！        ||\n");
			printf("===============================================\n");
		}
		fclose(fp);
	}
	else
	{
		system("cls");
		printf("\n");
		printf("===============================================\n");
		printf("||         两次输入的密码不一致，请重新输入  ||\n");
		printf("===============================================\n");
		register_fun(choice);
	}
}
